package com.chenglu.android.mine.login;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Surface;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.chenglu.android.BuildConfig;
import com.chenglu.android.MainActivity;
import com.chenglu.android.R;
import com.chenglu.android.bean.LoginBean;
import com.chenglu.android.bean.SmsCodeBean;
import com.chenglu.android.common.base.BaseFullScreenActivity;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.utils.CountDownTimerUtils;
import com.chenglu.android.common.utils.EdittextUtils;
import com.chenglu.android.common.utils.SPUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.common.utils.Utils;
import com.chenglu.android.mine.login.mvp.contract.LoginContract;
import com.chenglu.android.mine.login.mvp.presenter.LoginPresenter;
import com.chenglu.android.onelogin.OneKeyLoginActivity;
import com.chenglu.android.onelogin.OneLoginConstant;
import com.chenglu.android.onelogin.utils.ExecutorManager;
import com.chenglu.android.onelogin.utils.MockRequest;
import com.chenglu.android.onelogin.ScreenUtils;
import com.mobile.auth.gatewayauth.AuthRegisterViewConfig;
import com.mobile.auth.gatewayauth.AuthRegisterXmlConfig;
import com.mobile.auth.gatewayauth.AuthUIConfig;
import com.mobile.auth.gatewayauth.AuthUIControlClickListener;
import com.mobile.auth.gatewayauth.CustomInterface;
import com.mobile.auth.gatewayauth.PhoneNumberAuthHelper;
import com.mobile.auth.gatewayauth.ResultCode;
import com.mobile.auth.gatewayauth.TokenResultListener;
import com.mobile.auth.gatewayauth.model.TokenRet;
import com.mobile.auth.gatewayauth.ui.AbstractPnsViewDelegate;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author: lyj
 * @Created: by Copyright 2023 6 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：
 */
public class LoginActivity extends BaseFullScreenActivity<LoginPresenter> implements LoginContract.View {

    private static final String TAG = LoginActivity.class.getSimpleName();
    @BindView(R.id.cb_login_must)
    CheckBox cbLoginMust;
    @BindView(R.id.tv_login_user_agreement)
    TextView tvLoginUserAgreement;
    @BindView(R.id.tv_login_privacy_agreement)
    TextView tvLoginPrivacyAgreement;
    @BindView(R.id.tv_three_agreement)
    TextView tvThreeAgreement;
    @BindView(R.id.et_login_phone)
    EditText etLoginPhone;
    @BindView(R.id.iv_clear_phone)
    ImageView ivClearPhone;
    @BindView(R.id.et_login_pwd)
    EditText etLoginPwd;
    @BindView(R.id.iv_clear_pwd)
    ImageView ivClearPwd;
    @BindView(R.id.tv_sms_code)
    TextView tvSmsCode;
    @BindView(R.id.cb_login)
    CheckBox cbLogin;
    @BindView(R.id.tv_onelogin_user_agreement)
    TextView tvOneLoginUserAgreement;
    @BindView(R.id.tv_onelogin_privacy_agreement)
    TextView tvOneLoginPrivacyAgreement;
    @BindView(R.id.tv_change_login_mode)
    TextView tvChangeLoginMode;
    @BindView(R.id.btn_login)
    TextView btnLogin;
    @BindView(R.id.btn_one_login)
    TextView btnOneLogin;
    @BindView(R.id.iv_delete)
    ImageView ivDelete;
    @BindView(R.id.tv_moblie)
    TextView tvMoblie;

    //aliyunoneLogin
    private PhoneNumberAuthHelper mPhoneNumberAuthHelper;
    private TokenResultListener mTokenResultListener;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        mPresenter = new LoginPresenter();
        mPresenter.attachView(this);

        initLogin();


    }





    boolean isLogin;

    //账号密码登录
    private void initLogin() {
        EdittextUtils.setClearImage(etLoginPhone, ivClearPhone);
        EdittextUtils.setClearImage(etLoginPwd, ivClearPwd);

        cbLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    isLogin = true;
                    btnLogin.setBackground(getResources().getDrawable(R.drawable.corners_btn_login_default));
                } else {
                    isLogin = false;
                    btnLogin.setBackground(getResources().getDrawable(R.drawable.corners_btn_login_press));
                }
            }
        });
    }

    private void smsCode() {
        if (TextUtils.isEmpty(etLoginPhone.getText().toString().trim())) {
            ToastUtils.show("请输入手机号码");
            return;
        }
        if (!Utils.isMobileNO(etLoginPhone.getText().toString().trim())) {
            ToastUtils.show("输入的手机号码不正确");
            return;
        }

//        if(TextUtils.isEmpty(etLoginPwd.getText().toString().trim())){
//            ToastUtils.show("请输入验证码");
//            return;
//        }
        mPresenter.getSmsCode(etLoginPhone.getText().toString());


    }

    @Override
    public void getLogin(LoginBean bean) {
        if (bean.getCode()==Constant.HTTPCODE200) {

            SPUtils.setString(Constant.AaccessToken, bean.getData().getAccess_token());
//            SPUtils.setString(Constant.AaccessToken, "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJpc3N1c2VyIiwiYXVkIjoiYXVkaWVuY2UiLCJyb2xlX25hbWUiOiJ1c2VyIiwidXNlcl9pZCI6IjEwMDAwMDIyIiwidXNlcl9uYW1lIjoiMTUyNzgyMjA1OTgiLCJuaWNrX25hbWUiOiLnlKjmiLcwNTk4Iiwib2F1dGhfaWQiOiIiLCJkZXRhaWwiOnsiaWQiOjEwMDAwMDIyLCJwaWQiOjAsInBwaWQiOjAsInNlbGxlcklkIjowLCJjbGllbnRJZCI6bnVsbCwiYWNjb3VudCI6IlFRU1kwMTMwMiIsIm1vYmlsZSI6IjE1Mjc4MjIwNTk4Iiwibmlja05hbWUiOiLnlKjmiLcwNTk4IiwicmVhbE5hbWUiOm51bGwsImF2YXRhciI6Imh0dHBzOi8vdGVtcGxhdGVmaWxl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Zvcm1SZWRCYWciOjAuMH0sInRva2VuX3R5cGUiOiJhY2Nlc3NfdG9rZW4iLCJhY2NvdW50IjoiMTUyNzgyMjA1OTgiLCJjbGllbnRfaWQiOiJ3YXAiLCJleHAiOjE2OTc0MjA1OTgsIm5iZiI6MTY5NjgxNTc5OH0.u2HjmH3dJyhFYYuQZO5N_qFPRmxK3eT2ryJ7YTjDkzc");
            SPUtils.setString(Constant.refreshToken, bean.getData().getRefresh_token());
            SPUtils.setString(Constant.USERNAME, etLoginPhone.getText().toString());
            SPUtils.setBoolean("ISLOGIN", true);
            finish();
        } else {
            ToastUtils.show(bean.getMsg());
        }
    }

    @Override
    public void onRefreshLogin(LoginBean bean) {

    }

    private CountDownTimerUtils cdt;

    @Override
    public void getSmsCode(SmsCodeBean bean) {

        if (bean.getCode().equals(Constant.HTTPCODE200)) {
            ToastUtils.show(bean.getMsg());
            cdt = new CountDownTimerUtils(tvSmsCode, 60 * 1000, 1000);
            cdt.start();
//            new Thread(new TimeThread()).start();
//            mainHandler.sendEmptyMessage(H_HMAC_ENABLED);
        } else {
            ToastUtils.show(bean.getMsg());
        }
    }

    @Override
    public void getOneLogin(LoginBean bean) {

    }

    @OnClick({R.id.btn_one_login, R.id.tv_login_privacy_agreement, R.id.tv_three_agreement, R.id.tv_onelogin_user_agreement,
            R.id.tv_onelogin_privacy_agreement, R.id.tv_sms_code, R.id.tv_change_login_mode, R.id.btn_login, R.id.iv_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_one_login:
                break;
            case R.id.tv_login_privacy_agreement:

                break;
            case R.id.tv_three_agreement:
                break;
            case R.id.tv_onelogin_user_agreement:
                startActivity(new Intent(this,WebViewActivity.class)
                        .putExtra("url",Constant.HTTPYINSI)
                        .putExtra("name",getResources().getString(R.string.user_agreement)));
                break;
            case R.id.tv_onelogin_privacy_agreement:
                startActivity(new Intent(this,WebViewActivity.class)
                        .putExtra("url",Constant.HTTPYONGHU)
                        .putExtra("name",getResources().getString(R.string.privacy_agreement)));
                break;
            case R.id.tv_change_login_mode:
                if(Utils.checkSim(mContext)){
                    if(Utils.isEmulator()){
                        ToastUtils.show("不支持模拟器一键登录");
                    }else {
                        startActivity(new Intent(this, OneKeyLoginActivity.class));
                        finish();
                    }

                }else {
                    ToastUtils.show("请是否插入检查SIM卡");
                }


//                .putExtra(OneLoginConstant.THEME_KEY,OneLoginConstant.CUSTOM_XML)
                break;
            case R.id.tv_sms_code:
                smsCode();
                break;
            case R.id.btn_login:

                if (!isLogin) {
                    //  请求数据
                    ToastUtils.show("请同意协议");
                    return;
                }
                if (TextUtils.isEmpty(etLoginPhone.getText().toString().trim())) {
                    ToastUtils.show("请输入手机号码");
                    return;
                }
                if (TextUtils.isEmpty(etLoginPwd.getText().toString().trim())) {
                    ToastUtils.show("请输入验证码");
                    return;
                }

//                SPUtils.setString(Constant.AaccessToken, "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJpc3N1c2VyIiwiYXVkIjoiYXVkaWVuY2UiLCJyb2xlX25hbWUiOiJ1c2VyIiwidXNlcl9pZCI6IjEwMDAwMDIyIiwidXNlcl9uYW1lIjoiMTUyNzgyMjA1OTgiLCJuaWNrX25hbWUiOiLnlKjmiLcwNTk4Iiwib2F1dGhfaWQiOiIiLCJkZXRhaWwiOnsiaWQiOjEwMDAwMDIyLCJwaWQiOjAsInBwaWQiOjAsInNlbGxlcklkIjowLCJjbGllbnRJZCI6bnVsbCwiYWNjb3VudCI6IlFRU1kwMTMwMiIsIm1vYmlsZSI6IjE1Mjc4MjIwNTk4Iiwibmlja05hbWUiOiLnlKjmiLcwNTk4IiwicmVhbE5hbWUiOm51bGwsImF2YXRhciI6Imh0dHBzOi8vdGVtcGxhdGVmaWxlLnNsbXlrai5jb20vdXBsb2FkLyVFNSVCRSVBRSVFNCVCRiVBMSVFNSU5QiVCRSVFNyU4OSU4N18yMDIzMDEwNTE1NDczMi5wbmciLCJjaGFpbiI6IjAiLCJzY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AiOjE2OTc0MjA1OTgsIm5iZiI6MTY5NjgxNTc5OH0.u2HjmH3dJyhFYYuQZO5N_qFPRmxK3eT2ryJ7YTjDkzc");
//                SPUtils.setString(Constant.USERNAME, etLoginPhone.getText().toString());
//                SPUtils.setBoolean("ISLOGIN", true);
//                finish();
                mPresenter.getLogin(etLoginPhone.getText().toString(), etLoginPwd.getText().toString(), Constant.mobileSms);
                break;
            case R.id.iv_delete:
                finish();
                break;
        }
    }


}
